package com.why.catchMsg.service.impl;

import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.why.catchMsg.util.HttpClientUtils;
import com.why.catchMsg.service.CacheService;
import com.why.catchMsg.service.HttpClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class HttpClientServiceImpl implements HttpClientService{

    @Autowired
    CacheService cacheService;

    @Override
    public String get(String url, Map<String, Object> map) {
        if(map==null){
            map=new HashMap<String,Object>();
        }
        map.put("session_id",cacheService.getSessionId());
        String result= HttpClientUtils.stdRequest(url, RequestMethod.GET,map);
        return result;
    }

    @Override
    public String post(String url, Map<String, Object> map) {
        if(map==null){
            map=new HashMap<String,Object>();
        }
        map.put("session_id",cacheService.getSessionId());
        String result= HttpClientUtils.stdRequest(url, RequestMethod.POST,map);
        return result;
    }

    @Override
    public String put(String url, Map<String, Object> map) {
        if(map==null){
            map=new HashMap<String,Object>();
        }
        map.put("session_id",cacheService.getSessionId());
        String result= HttpClientUtils.stdRequest(url, RequestMethod.PUT,map);
        return result;
    }

    @Override
    public String delete(String url, Map<String, Object> map) {
        if(map==null){
            map=new HashMap<String,Object>();
        }
        map.put("session_id",cacheService.getSessionId());
        String result= HttpClientUtils.stdRequest(url, RequestMethod.DELETE,map);
        return result;
    }

    @Override
    public String postJson(String url, String body) {
        String result=HttpClientUtils.stdRequestBody(cacheService.getSessionId(),url,RequestMethod.POST,body);
        return result;
    }

    @Override
    public String putJson(String url, String body) {
        String result= HttpClientUtils.stdRequestBody(cacheService.getSessionId(),url,RequestMethod.PUT,body);
        return result;
    }

    @Override
    public String deleteJson(String url, String body) {
        String result= HttpClientUtils.stdRequestBody(cacheService.getSessionId(),url,RequestMethod.DELETE,body);
        return result;
    }

    @Override
    public String down(String url, Map<String, Object> map) {
        if(!map.isEmpty()){
            url+="?";
        }
        for (Map.Entry<String,Object> entry : map.entrySet()) {
            url=url+entry.getKey()+"="+entry.getValue()+"&";
        }
        url=url.substring(0,url.length()-1);
        log.info("url:{}",url);
        File file = new File("d:/baidu3.png");
        try {
            HttpClientUtil.down(HttpConfig.custom().url(url).out(new FileOutputStream(file)));
        } catch (HttpProcessException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (file.exists()) {
            System.out.println("图片下载成功了！存放在：" + file.getPath());
        }
        return "test";
    }
}
